﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.PhoneCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.PhoneCommandHandler
{
    public class AddPhoneCommandHandler : IRequestHandler<AddPhoneCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_PhoneInfo> phoneRep;
        private readonly IMapper mapper;
        public AddPhoneCommandHandler(IBaseRepository<SU_PhoneInfo> phoneRep, IMapper mapper)
        {
            this.phoneRep = phoneRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(AddPhoneCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var model = mapper.Map<SU_PhoneInfo>(request);
            model.PhoneType=request.PhoneType;
            model.ProductionCode=request.ProductionCode;
            var res= phoneRep.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
